package com.afsadmedia.pregnancy;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.xml.datatype.Duration;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Add_WeightManagement extends Activity {
	Spinner spinnerunits;
	TextView dateset;
	Button ok, cancel;
	EditText ETweight;
	
	double weight;
	int abc;
	String pqr;

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		calculatedatetime();
		super.onStart();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_weightmanagement);
		
//		Constants.addeddate = new ArrayList<String>();
//		Constants.addedweight =new ArrayList<String>();

		calculatedatetime();

		spinnerunits = (Spinner) findViewById(R.id.spinner_units);
		dateset = (TextView) findViewById(R.id.tv_date_addweight);
		ok = (Button) findViewById(R.id.button_ok_addweight);
		cancel = (Button) findViewById(R.id.button_cancel_addweight);
		ETweight=(EditText)findViewById(R.id.et_addweightmanagemnt);

		dateset.setText(Constants.calculatedate);

		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();

			}
		});

		ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			
				 
				 if(ETweight.getText().toString().equals("")){
					 Toast.makeText(getApplicationContext(), "Please Enter Weight", Toast.LENGTH_LONG).show();
					 
				 }else{
					 
					 if(Constants.selectunit.equals("lb")){
						 
						 
						 
						 abc = Integer.parseInt(ETweight.getText().toString()); 
						 
						
						 
						 DecimalFormat dtime = new DecimalFormat("#.##"); 
						 weight = Double.valueOf(dtime.format(abc * 0.45359237));
						 
						 Toast.makeText(getApplicationContext(), String.valueOf(weight), Toast.LENGTH_LONG).show();
						 
		
					 }else{
						 
						 abc = Integer.parseInt(ETweight.getText().toString());
						 
						 weight = abc;
						 
						 Toast.makeText(getApplicationContext(), String.valueOf(weight), Toast.LENGTH_LONG).show();
						 
					 }
					 
						Constants.selectweight = ETweight.getText().toString()  +  Constants.selectunit;
						Constants.addedweight.add(Constants.selectweight);
						Constants.addeddate.add(Constants.calculatedate);
						 Log.i("SELCT",Constants.selectweight);
//				Intent i = new Intent(Add_WeightManagement.this,WeightManagement.class);
//				startActivity(i);
//				finish();
				 }

			}
		});

		ArrayAdapter<CharSequence> adapter = ArrayAdapter
				.createFromResource(this, R.array.units_array,
						android.R.layout.simple_spinner_item);

		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinnerunits.setAdapter(adapter);
		spinnerunits.setOnItemSelectedListener(new MyOnItemSelectedListener());

	}

	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
				long arg3) {
			// TODO Auto-generated method stub
			 Constants.selectunit = parent.getItemAtPosition(pos).toString();
			 Log.i("UserLevelData",Constants.selectunit);

		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	}

	private void calculatedatetime() {
		Calendar calendar = Calendar.getInstance();
		int current_date = calendar.get(Calendar.DATE);
		int current_month = calendar.get(Calendar.MONTH);
		current_month = current_month + 1;
		int current_year = calendar.get(Calendar.YEAR);
		Date time = calendar.getTime();

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
		cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));

		Log.v("Date",
				String.valueOf(current_date) + "/"
						+ String.valueOf(current_month) + "/"
						+ String.valueOf(current_year));

		Constants.calculatedate = current_date + "/" + current_month + "/"
				+ current_year;

	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		finish();
		super.onPause();
	}

}
